/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableCampaignHourlyV2 {
public static final String KEYSPACE                         =   "AdsData";
public static final String ColumFamly                       =   "BannerViewCountCf";
public static final String ColumFamlyBannerClickCountCf     =   "BannerClickCountCf";
public static final String ColumFamlyBannerViewCountCf      =   "BannerViewCountCf";

public static final String ColumFamlyCampaignClickCountCf       =   "CampaignCountCf";
public static final String ColumFamlyCampaignViewCountCf        =   "CampaignViewCountCf";

public StringBuilder handleData() throws ParseException, InterruptedException
{
    StringBuilder sbff = new StringBuilder();
    //handle data by v3_cam_date    
    CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
    cassAct.initReadWirte(KEYSPACE);
    MysqlDataManagerMain mysqlCPM = new MysqlDataManagerMain();
    List listcamps = mysqlCPM.getCampaignCPCandCPCPLUS();
    try{
        String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
        //String currentDate = CONSTANT.Date;
        String[] arrStr = new String[3];
        arrStr = currentDate.split("-");
        String changeDay = arrStr[2]+"/"+arrStr[1]+"/"+arrStr[0];                
        Iterator it=listcamps.iterator();
        while(it.hasNext())
        {
            String key = (String)it.next();
            String str = PhuongThreadV1.handleData(cassAct, key, changeDay, key, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf);
            sbff.append(str);
            //PhuongThreadV1 test = new PhuongThreadV1(cassAct, key, changeDay, changeDay, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf,sbff);
        }
       
    } catch (Exception ex)
    {
        System.out.println("error:"+ex.getMessage());
    }
   
    return sbff;
}

public static String getClickCampaignHour(CassandraAction cassAct,String startDay,String finishDay,String key,String columnFamilyClick,String columnFamilyView) throws ParseException{

        StringBuilder sb = new StringBuilder();      
        String dayStart = startDay.split("/")[0];
        String dayLast = finishDay.split("/")[0];
        String prefix = "-" +startDay.split("/")[1]+"-"+startDay.split("/")[2];

        String prefixDB = startDay.split("/")[2] + "-" + startDay.split("/")[1] + "-";

        for(int i = Integer.valueOf(dayStart) ; i <= Integer.valueOf(dayLast) ;i++)
        {
            for(int hour =0 ;hour <=23 ;hour ++)
            {
                if(hour < 10)
                {
                    String date = "0"+ hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                    sb.append(str).append(",");
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                } else {
                    String date = hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                    sb.append(str).append(",");
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                }
            }
        }
        return sb.toString();
    }

}
